package com.huaxin.hxenum.enums;

import com.huaxin.hxenum.base.BaseCodeEnum;
import com.huaxin.hxenum.base.Colorful;

/**
 * 优先级（使用数字 code）
 */
public enum Priority implements BaseCodeEnum<Integer>, Colorful {

    LOW(1, "低", "#28a745"),
    MEDIUM(2, "中", "#6c757d"),
    HIGH(3, "高", "#ffc107"),
    CRITICAL(4, "危急", "#dc3545");

    private final int code;
    private final String displayName;
    private final String color;

    Priority(int code, String displayName, String color) {
        this.code = code;
        this.displayName = displayName;
        this.color = color;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    @Override
    public String getColor() {
        return color;
    }

    /**
     * 根据 code 获取枚举实例
     */
    public static Priority of(int code) {
        for (Priority p : values()) {
            if (p.code == code) {
                return p;
            }
        }
        return null;
    }

    /**
     * 校验 code 是否有效
     */
    public static boolean isValid(int code) {
        return of(code) != null;
    }

    /**
     * 兼容字符串输入（用于校验）
     */
    public static boolean isValid(String codeStr) {
        if (codeStr == null || codeStr.trim().isEmpty()) {
            return false;
        }
        try {
            int code = Integer.parseInt(codeStr.trim());
            return isValid(code);
        } catch (NumberFormatException e) {
            return false;
        }
    }
}